<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    name: {
      type: String,
      default: ''
    },
    chartData: {
      type: Array,
      required: true
    },
    xName: {
      type: Array,
      default: () => ['空闲房间', '已使用房间']
    },
    count: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: this.name,
          subtext: '总房间数：' + this.count
        },
        tooltip: {
          trigger: 'item'
          // formatter: '{a} <br/>{b} : {c}台 ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: this.xName
        },
        series: [
          {
            name: this.name,
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: this.chartData,
            // [
            //   { value: 1000, name: '空闲房间' },
            //   { value: 500, name: '已使用房间' }
            // ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
